<?php
/**
 * Object Relational Persistence Mapping Class for table: rad_ticket_item
 *
 * @package	com.athena
 * @author	LHV
 */
class RadTicketItem extends ORDataObject {

	/**#@+
	 * Fields of table: rad_ticket_item mapped to class members
	 */
	var $rad_ticket_item_id	= '';
	var $rad_ticket_id		= '';
	var $service_id    		= '';
	var $original_price		= '';
	var $discount      		= '';
	var $insurance_price    = '';
	var $patient_price		= '';
	var $is_done       		= '';
	/**#@-*/

	const  STATUS_DONE         =   1;
	const  STATUS_IN_PROGRESS  =   0;

	/**
	 * DB Table
	 */
	var $_table = 'rad_ticket_item';

	/**
	 * Primary Key
	 */
	var $_key = 'rad_ticket_item_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'RadTicketItem';

	/**
	 * Handle instantiation
	 */
	function RadTicketItem() {
		parent::ORDataObject();
	}

	/**
	 * Set data to the object. This is a wrapper of populateArray method.
	 *
	 * @param int $radTicketId
	 * @param int $serviceId
	 * @param decimal $price
	 * @param decimal $discount
	 * @param decimal $insPrice
	 * @param decimal $patPrice
	 * @param tinyint $done
	 */
	public function setData($radTicketId, $serviceId, $price, $insPrice, $patPrice, $done) {
        $data =   array(
            'rad_ticket_id'     =>  $radTicketId,
            'service_id'        =>  $serviceId,
            'original_price'    =>  $price,
            'insurance_price'   =>  $insPrice,
            'patient_price'     =>  $patPrice,
            'is_done'           =>  $done
        );
        $this->populateArray($data);
	}

	public function getServicesByTicket($radTicketId, $toArrayKeyValue = false, $showItemStatus = false) {
        $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
		$sql    =   array(
            'cols'  =>  "$this->_key, s.$field as name, rad_ticket_id, t.service_id, t.original_price, t.discount
                    ,t.insurance_price, t.patient_price, t.is_done",
            'from'  =>  "$this->_table t INNER JOIN service s ON (t.service_id=s.service_id)",                    
            'where' =>  "rad_ticket_id=$radTicketId"
        );
        $ds =   new Datasource_sql();
        $ds->setup($this->_db, $sql);

        if ($toArrayKeyValue){
        	if ($showItemStatus) {
        		$arr  =   $ds->toArray();
        		$ret  =   array();
                foreach ($arr as $item) {
                    $ret[$item["$this->_key"]]  =   RadUtils::generateRadItemName($item['name'], $item['is_done'], $showItemStatus);
                }
                return $ret;
        	}
        	return $ds->toArray("$this->_key", 'name');
        }
        return $ds->toArray();
	}

	public function deleteServicesByTicket($radTicketId) {
        $sql    =   "DELETE FROM $this->_table WHERE rad_ticket_id=$radTicketId";
        $this->_execute($sql);
	}

	public function getServiceId() {
		return $this->service_id;
	}

	public function getRadTicketId() {
		return $this->rad_ticket_id;
	}

	private function deleteRadResultByTicket($radTicketId) {

	}

	public function updateItemStatus($radTicketItemId , $status = 1) {
        $sql    =   "UPDATE $this->_table SET is_done=$status WHERE rad_ticket_item_id='$radTicketItemId'";
        $this->_execute($sql);
	}

	public function getPriceInfo() {
        $price  =   ORDataObject::factory('RadAdditionalServiceItem')->getPriceInfoByTicketItem($this->rad_ticket_item_id);

        $priceInfo['original_price']    +=  $price + $this->original_price;
        $priceInfo['insurance_price']   +=  $this->insurance_price;
        $priceInfo['patient_price']     +=  $price + $this->patient_price;

        return $priceInfo;
	}

	public function getItemInfo($radTicketItemId) {
        $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $sql    =   array(
            'cols'  =>  "$this->_key, s.$field as name, rad_ticket_id, t.service_id, t.original_price, t.discount
                    ,t.insurance_price, t.patient_price, t.is_done",
            'from'  =>  "$this->_table t INNER JOIN service s ON (t.service_id=s.service_id)",
            'where' =>  "$this->_key='$radTicketItemId'"
        );
        $ds =   new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
	}
}
?>
